package com.letv.whatslive.web.util.displaycontrol;

import com.google.common.collect.Lists;
import com.letv.whatslive.common.annotation.DisplayControlAnnotation;
import com.letv.whatslive.model.utils.TransferField;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;

/**
 * Created by shenyue1 on 2016/5/24.
 */
class FieldInfo implements Comparable<FieldInfo>{
    private String displayName;

    public String getDisplayName() {
        return displayName;
    }

    public Integer getOrder() {
        return order;
    }

    public Field getField() {
        return field;
    }

    private Integer order;
    private Field field;

    public Integer getTransfer() {
        return transfer;
    }

    private Integer transfer;
    public FieldInfo(String displayName,Integer order,Field field,Integer transfer){
        this.displayName=displayName;
        this.order=order;
        this.field=field;
        this.transfer=transfer;
    }
    public FieldInfo(){}

    public int compareTo(FieldInfo o) {
        if(o==null)
            return 1;
        if(order>o.order)
            return 1;
        else if(order<o.order)
            return -1;
        else
            return 0;
    }
}
public class DisplayObjectInOrderImpl implements DisplayObjectInOrder {
    private Class targetClass;
    private List<FieldInfo> fieldInfos;
//    private TransferField<String> transferField;
    public DisplayObjectInOrderImpl(){

    }
    public DisplayObjectInOrderImpl(Class targetClass/*,TransferField<String> transferField*/){
        this.targetClass=targetClass;
//        this.transferField=transferField;
        fieldInfos= Lists.newArrayList();
        Field[] fields=targetClass.getDeclaredFields();
        for(Field field:fields){
            DisplayControlAnnotation displayControlAnnotation=field.getAnnotation(DisplayControlAnnotation.class);
            if(displayControlAnnotation!=null){
                field.setAccessible(true);
                String displayName=displayControlAnnotation.displayName();
                Integer order=displayControlAnnotation.order();
                Integer transfer=displayControlAnnotation.transfer();
                FieldInfo fieldInfo=new FieldInfo(displayName,order,field,transfer);
                this.fieldInfos.add(fieldInfo);
            }
        }
        Collections.sort(this.fieldInfos);
    }
    public List<String> displayFiledNames() {
        List<String> filedNames=Lists.newArrayList();
        for(FieldInfo fieldInfo:fieldInfos){
            filedNames.add(fieldInfo.getDisplayName());
        }
        return filedNames;
    }

    public List<String> displayFileds(Object object,TransferField<String> transferField) {
        if(object.getClass()!=targetClass)
            return null;
        List<String> fields=Lists.newArrayList();
        for(FieldInfo fieldInfo:fieldInfos){
            try {
                Object o=fieldInfo.getField().get(object);
                if(o!=null){
                    String fieldValue=o.toString();
                    if(fieldInfo.getTransfer()!=0){
                        if(transferField!=null){
                            fieldValue=transferField.transfer(fieldInfo.getField(),o);
                        }
                    }
                    fields.add(fieldValue);
                }
                else
                    fields.add("");
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e.getMessage());
            }
        }
        return fields;
    }
}
